652C - Foe Pairs - CodeForces Solution


combinatorics sortings two pointers *1800

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
/*----------------------------------------------------------------------------------------------*/
#define xx cout<<"I'm Here\n";
#define int long long
#define vi vector<int>
#define all(v) v.begin(),v.end()
#define prntv(arr) for(auto &a:arr) cout<<a<<" ";cout<<"\n";
#define prntp(arr) for(auto &a:arr){cout<<a.first<<" "<<a.second; cout<<"\n";}
#define prnt2d(v)  for(auto &arr:v){ for(auto &n:arr) cout<<n<<" "; cout<<"\n";}
#define pn(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {cout<<"\n";}template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) {cout << *it << " = " << a <<" ";err(++it, args...);}
template <typename T1, typename T2> istream &operator>>(istream &istream, pair<T1, T2> &p){return (istream >> p.first >> p.second);}template <typename T> istream &operator>>(istream &istream, vector<T> &v){for (auto &it : v)cin >> it;return istream;}
template <typename T1, typename T2> ostream &operator<<(ostream &ostream, const pair<T1, T2> &p){return (ostream << p.first << " " << p.second);}template <typename T> ostream &operator<<(ostream &ostream, const vector<T> &c){for(auto &it : c)cout << it << " ";return ostream;}
template <typename T> void prnt(T &&t){cout<<t<<"\n";} template <typename T, typename... Args> void prnt(T &&t, Args &&...args){cout << t << " ";prnt(forward<Args>(args)...);}
struct custom_hash{static uint64_t splitmix64(uint64_t x){x += 0x9e3779b97f4a7c15;x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;x = (x ^ (x >> 27)) * 0x94d049bb133111eb;return x ^ (x >> 31);}size_t operator()(uint64_t x) const{static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();return splitmix64(x + FIXED_RANDOM);}};
constexpr long long N = 1e7+10;
constexpr long long INF = 4e18;
constexpr long double EPS = 1e-9;
constexpr long long MOD = 998244353; // 1e9 + 7;
/*----------------------------------------------------------------------------------------------*/

void solve(int tt){
  int n,m;cin>>n>>m;
  vi v(n);cin>>v;

  map<int,int> mp;
  map<int,int> pos;
  for(int i=0;i<n;i++){
    pos[v[i]]=i;
    mp[i]=n;
  }

  //suppose each i to be L, then store nearest R
  while(m--){
    int a,b;cin>>a>>b;
    int x=pos[a],y=pos[b];
    if(x>y) swap(x,y);
    mp[x]=min(mp[x],y);
  }

//   prntp(mp);


  int ans=0;
  int r=n; //to maintain ek L ke liye knha tk ja skta h R, kisi piche wale L ko apne aage wali L ke R se aage nhi jana h 
  //count for each L, valid R (R-L-1+1);
  for(int i=n-1;i>=0;i--){
    r=min(r,mp[i]); 
    ans+=r-i;
  }
  prnt(ans);
}

int32_t main(){
  ios_base::sync_with_stdio(false);cin.tie(NULL);
  //cout<<fixed<<setprecision(9);
 
  int tt=1;
//   cin>>tt;
  for(int i=1;i<=tt;i++)
  solve(i);
  
  return 0;
}


Comments

Submit
0 Comments
More Questions

1749A - Cowardly Rooks
1749C - Number Game
1749B - Death's Blessing
1749D - Counting Arrays
1447B - Numbers Box
1594D - The Number of Imposters
984B - Minesweeper
837A - Text Volume
1566C - MAX-MEX Cut
1546A - AquaMoon and Two Arrays
897B - Chtholly's request
1363B - Subsequence Hate
437B - The Child and Set
1256B - Minimize the Permutation
733B - Parade
172A - Phone Code
148D - Bag of mice
421A - Pasha and Hamsters
1393A - Rainbow Dash Fluttershy and Chess Coloring
980E - The Number Games
219B - Special Offer Super Price 999 Bourles
560B - Gerald is into Art
322B - Ciel and Flowers
801B - Valued Keys
975C - Valhalla Siege
518B - Tanya and Postcard
514B - Han Solo and Lazer Gun
898B - Proper Nutrition
9C - Hexadecimal's Numbers
1265B - Beautiful Numbers